package com.weixin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.weixin.config.WeiXinProperties;
import com.weixin.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zjd
 * @Title:
 * @Description:
 * @date 2021/4/1611:39
 */
@Api("企业微信-效率工具-日历")
@RestController
@RequestMapping("/v1.0/wei-xin/cgi-bin/calendar")
public class ScheduleController {

    @Autowired
    private RestTemplate restTemplate;
    @Resource
    private ObjectMapper objectMapper;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("创建日历")
    @PostMapping("/create")
    public ResponseEntity create(@RequestParam("name") String name,
                                 @RequestParam("owner") String owner,
                                 @RequestParam("userList") List<String> users) throws IOException {
        Map map= new HashMap<String, Object>();
        map.put("name",name);
        map.put("owner",owner);
        map.put("userlist",users);
        String jsonStr = objectMapper.writeValueAsString(map);
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/oa/calendar/add?");
        String access_token = (String) redisUtil.get("access_token_application");
        urlBuilder.append("access_token="+access_token);
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求类型
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 封装参数和头信息
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonStr, httpHeaders);
        String result = restTemplate.postForObject(urlBuilder.toString(),httpEntity, String.class);
        Map<String, Object> readValue = objectMapper.readValue(result, HashMap.class);
        return resultMap(readValue,1,null);
    }

    @ApiOperation("获取群聊")
    @GetMapping("/{id}")
    public ResponseEntity get(@RequestParam("id") String id) throws IOException {
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/appchat/get?");
        String access_token = (String) redisUtil.get("access_token_application");
        urlBuilder.append("access_token="+access_token);
        urlBuilder.append("&chatid="+id);
        String result = restTemplate.getForObject(urlBuilder.toString(), String.class);
        Map<String, Object> readValue = objectMapper.readValue(result, HashMap.class);
        return resultMap(readValue,1,null);
    }

    @ApiOperation("查询应用消息发送统计")
    @PostMapping("/getStatistics")
    public ResponseEntity getStatistics(@RequestParam("type") String type) throws IOException {
        Map map= new HashMap<String, Object>();
        map.put("time_type",type);
        String jsonStr = objectMapper.writeValueAsString(map);
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/message/get_statistics?");
        String access_token = (String) redisUtil.get("access_token_application");
        urlBuilder.append("access_token="+access_token);
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置请求类型
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        // 封装参数和头信息
        HttpEntity<String> httpEntity = new HttpEntity<>(jsonStr, httpHeaders);
        String result = restTemplate.postForObject(urlBuilder.toString(),httpEntity, String.class);
        Map<String, Object> readValue = objectMapper.readValue(result, HashMap.class);
        return resultMap(readValue,1,null);
    }



    private ResponseEntity resultMap(Map<String, Object> resultMap, Integer type,String key){
        for (Map.Entry<String, Object> entry : resultMap.entrySet()) {
            if (entry.getKey().equals("errcode")){
                Integer integer = (Integer) resultMap.get("errcode");
                if (integer==0){
                    ResponseEntity responseEntity = type==0 ? new ResponseEntity(resultMap.get(key), HttpStatus.OK): new ResponseEntity(resultMap, HttpStatus.OK);
                    return responseEntity;
                }
            }
        }
        return new ResponseEntity(resultMap,HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
